package com.woniuxy.info.sys.controller;

import com.woniuxy.info.sys.entity.Informations;
import com.woniuxy.info.sys.page.Pages;
import com.woniuxy.info.sys.result.Result;
import com.woniuxy.info.sys.result.enums.impl.ResultCode;
import com.woniuxy.info.sys.service.IInformationsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * 资讯信息前端控制器
 */
@Api(tags = "资讯信息控制器")
@RestController
@RequestMapping("/informations")
public class InformationsController {

    @Autowired
    private IInformationsService informationsService;
    @ApiOperation("分页查询资讯列表")
    @GetMapping("/query")
    public Result queryInfoList(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "5") Integer size) {
        Pages<Informations> pages = informationsService.findByPage(current, size);
        return Result.ok(pages);
    }
    @ApiOperation("资讯详情")
    @GetMapping("{id}")
    public Result<Informations> findInfoById(
            @PathVariable("id")
            @ApiParam(name ="id",value ="资讯ID",required = true) Integer id){
        Informations infos = informationsService.findInfoById(id);
        if (Objects.isNull(infos)) {
            return Result.fail(ResultCode.RESULE_DATA_NONE);
        }
        return Result.ok(infos);
    }
    @PutMapping("/reply/count/{infoId}")
    @ApiOperation("修改资讯评论数量")
    public Result<Integer> updateReplyCount(
            @PathVariable("infoId")
            @ApiParam(name ="infoId",value ="资讯ID",required = true) Integer infoId){
        Integer replyCount = informationsService.updateReplyCount(infoId);
        return Result.ok(replyCount);
    }

}
